package com.file.word;

import cn.hutool.core.io.resource.ClassPathResource;
import com.aspose.words.Document;
import com.aspose.words.License;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.net.URL;

/**
 * aspose 授权
 */
@Slf4j
public class WordLib {
    private static String TEST_LICENSE_FILE_NAME = "Aspose.license.lic";

    public static void setUnlimitedLicense() {
        try {
            //  URL licenseFileNameURL = WordLib.class.getResource("/km/word/Aspose.license.lic");
            ClassPathResource resource = new ClassPathResource("word/Aspose.license.lic");
            URL licenseFileNameURL = resource.getUrl();
            String savePath = java.net.URLDecoder.decode(licenseFileNameURL.toString(), "utf-8");
            String licenseFileName = savePath.toString().substring(6);
            if (new File(licenseFileName).exists()) {
                License license = new License();
                license.setLicense(licenseFileName);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("aspose 授权异常");
        }
    }

    /**
     * 更新阈值
     *
     * @param document
     * @throws Exception
     */
    public static void mergeField(Document document, String fieldname, Object fieldvalue) throws Exception {
        document.getMailMerge().execute(new String[]{fieldname}, new Object[]{fieldvalue});
    }

}
